<?php

error_reporting(E_ALL);

/**
 * untitledModel - class.Edition.php
 *
 * $Id$
 *
 * This file is part of untitledModel.
 *
 * Automatically generated on 26.02.2009, 15:28:43 with ArgoUML PHP module 
 * (last revised $Date: 2008-04-19 08:22:08 +0200 (Sat, 19 Apr 2008) $)
 *
 * @author firstname and lastname of author, <author@example.org>
 */

if (0 > version_compare(PHP_VERSION, '5')) {
    die('This file was generated for PHP 5');
}

/**
 * Short description of class Edition
 *
 * @access public
 * @author firstname and lastname of author, <author@example.org>
 */
class Edition implements ObjetPanier
{
    // --- ASSOCIATIONS ---
    // generateAssociationEnd :     // generateAssociationEnd :     // generateAssociationEnd :     // generateAssociationEnd : 

    // --- ATTRIBUTES ---

    /**
     * Short description of attribute IDRevue
     *
     * @access public
     * @var Integer
     */
    private $IDRevue = null;

    /**
     * Short description of attribute Titre
     *
     * @access public
     * @var String
     */
    private $Titre = null;

    /**
     * Short description of attribute Descriptif
     *
     * @access public
     * @var String
     */
    private $Descriptif = null;

    /**
     * Short description of attribute PrixVente
     *
     * @access public
     * @var Integer
     */
    private $PrixVente = null;

    /**
     * Short description of attribute Date
     *
     * @access public
     * @var Date
     */
    private $Date = null;

    /**
     * Short description of attribute URL
     *
     * @access public
     * @var String
     */
    private $URL = null;
    
    private $journal;
    
    private $ISSN;
    
    public function getDate(){
    	return $this->Date;
    }
    
    public function setDate($Date){
    	$this->Date=$Date;
    }
    
    public function getDescriptif(){
    	return $this->Descriptif;
    }
    
    public function setDescriptif($Descriptif){
    	$this->Descriptif=$Descriptif;
    }
    
    public function getIDRevue(){
    	return $this->IDRevue;
    }
    
    public function setIDRevue($IDRevue){
    	$this->IDRevue = $IDRevue;
    }
    
    public function getJournal(){
    	return $this->journal;
    }
    
    public function setJournal (Journal $j){
    	$this->journal = $j;
    }
    
    public function getPrixVente(){
    	return $this->PrixVente;
    }
    
    public function setPrixVente($PrixVente){
    	$this->PrixVente = $PrixVente;
    }
    
    public function getTitre(){
    	return $this->Titre;
    }
    
    public function setTitre($Titre){
    	$this->Titre = $Titre;
    }
    
    public function getURL(){
    	return $this->URL;
    }
    public function setURL($URL){
    	$this->URL=$URL;
    }
    
    public function getPrix(){
    	return $this->getPrixVente();
    }
    
    public function getIDEdition(){
    	return $this->getIDRevue();
    }
    
    public function getLogo(){
    	return $this->getURL();
    }
    
    public function setISSN($ISSN){
    	$this->ISSN= $ISSN;
    }
    
    public function getISSN(){
    	return $this->ISSN;
    }

    // --- OPERATIONS ---

    /**
     * Short description of method afficherUne
     *
     * @access public
     * @author firstname and lastname of author, <author@example.org>
     * @return mixed
     */
    public function afficherUne()
    {
    }
    
    /**
     * Fonction que recupére tout les éditions d'un journal en base de données
     * 
     * @return unknown_type
     */
	public static function recupAll($IDJournal)
	{
		
		$i = 0;
		$result = Array();
		
		// Récupération de la connexion et préparation de la requête.
		$tbh = ConnexionBD::getInstance();
		
		$stat = $tbh->prepare("SELECT * FROM edition WHERE id_journal = :id_journal");
		$array = array(':id_journal' => $IDJournal);
		$stat->execute($array);
		$arrValues = $stat->fetchAll(PDO::FETCH_ASSOC);
		foreach ($arrValues as $row){
			$edition = new Edition();
			$edition->setIDRevue($row['id_edition']);
			$edition->setTitre($row['titre']);
			$edition->setDescriptif($row['descriptif']);
			$edition->setPrixVente($row['prix_vente']);
			$edition->setDate($row['date_edition']);
			$edition->setURL($row['URL']);
			$edition->setISSN($row['ISSN']);
						
			$result[$i]=$edition;
			$i++;
		}
		
		return $result;
	}
	
	public static function recupEdition($IDEdition){
		
		// Récupération de la connexion et préparation de la requête.
		$tbh = ConnexionBD::getInstance();
		
		$stat = $tbh->prepare("SELECT * FROM edition WHERE id_edition = :id_edition");
		$array = array(':id_edition' => $IDEdition);
		$stat->execute($array);
		
		$arrValues = $stat->fetchAll(PDO::FETCH_ASSOC);
		foreach ($arrValues as $row){
			$result = new Edition();
			$result->setIDRevue($row['id_edition']);
			$result->setTitre($row['titre']);
			$result->setDescriptif($row['descriptif']);
			$result->setPrixVente($row['prix_vente']);
			$result->setDate($row['date_edition']);
			$result->setURL($row['URL']);
			$result->setISSN($row['ISSN']);
			$journal = new Journal();
			$journal->recup($row['id_journal']);	
			$result->setJournal($journal);					
		}
		
		return $result;
	}
	
	public function recupLastEdition($IDJournal){
				// Récupération de la connexion et préparation de la requête.
		$tbh = ConnexionBD::getInstance();
		
		$stat = $tbh->prepare("SELECT * FROM edition WHERE id_journal = :id_journal and date_edition >= SUBDATE(CURDATE(), INTERVAL :day DAY) ORDER BY date_edition DESC LIMIT 1");
		//						."and date_edition=(SELECT MAX(date_edition) WHERE id_journal = ".$IDJournal.")");
		$array = array(':id_journal' 	=> 	$IDJournal,
						':day'			=>	7
		);
		$stat->execute($array);
		
		$arrValues = $stat->fetchAll(PDO::FETCH_ASSOC);
		foreach ($arrValues as $row){

			$this->setIDRevue($row['id_edition']);
			$this->setTitre($row['titre']);
			$this->setDescriptif($row['descriptif']);
			$this->setPrixVente($row['prix_vente']);
			$this->setDate($row['date_edition']);
			$this->setURL($row['URL']);
			$this->setISSN($row['ISSN']);
			$journal = new Journal();
			$journal->recup($row['id_journal']);	
			$this->setJournal($journal);					
		}
		
	}
	
	public static function recupAllLastSeven($IDJournal){
		
		$i = 0;
		$result = Array();
		
		// Récupération de la connexion et préparation de la requête.
		$tbh = ConnexionBD::getInstance();
		
		$stat = $tbh->prepare("SELECT * FROM edition WHERE id_journal = :id_journal and date_edition >= SUBDATE(CURDATE(), INTERVAL :day DAY) ORDER BY date_edition DESC");
		$array = array(	':id_journal' 	=> 	$IDJournal,
						':day'			=>	7			
		);
		$stat->execute($array);
		$arrValues = $stat->fetchAll(PDO::FETCH_ASSOC);
		foreach ($arrValues as $row){
			$edition = new Edition();
			$edition->setIDRevue($row['id_edition']);
			$edition->setTitre($row['titre']);
			$edition->setDescriptif($row['descriptif']);
			$edition->setPrixVente($row['prix_vente']);
			$edition->setDate($row['date_edition']);
			$edition->setURL($row['URL']);
			$edition->setISSN($row['ISSN']);
						
			$result[$i]=$edition;
			$i++;
		}
		
		return $result;
	}
	
	public function getJournalNom(){
		
		// Récupération de la connexion et préparation de la requête.
		$tbh = ConnexionBD::getInstance();
		
		return $this->getJournal()->getNom();

	}
	
	public static function getJournalDuJour($journal){
		$tbh = ConnexionBD::getInstance();
		
		$stat = $tbh->prepare("SELECT * FROM edition WHERE id_journal = :id_journal and date_edition =:date");
		$array = array(	':id_journal' 	=> 	$journal,
						':date'			=>	date("Y-m-d")			
		);
		$stat->execute($array);
		
		if ($stat->rowCount()==0)
			return null;
		
		$arrValues = $stat->fetchAll(PDO::FETCH_ASSOC);
		foreach ($arrValues as $row){
			$edition = new Edition();
			$edition->setIDRevue($row['id_edition']);
			$edition->setTitre($row['titre']);
			$edition->setDescriptif($row['descriptif']);
			$edition->setPrixVente($row['prix_vente']);
			$edition->setDate($row['date_edition']);
			$edition->setURL($row['URL']);
			$edition->setISSN($row['ISSN']);
		}
		
		return $edition;
	}
	
	public function export(){
		$tbh = ConnexionBD::getInstance();
		
		$journal = new Journal();
		$journal->recup($this->IDRevue);
		
		
		$stat = $tbh->prepare("INSERT INTO edition set id_edition='', titre=:titre, prix_vente=:vente,
			date_edition=:date, descriptif=:descriptif, URL=:url, id_journal=:id_journal, ISSN=:issn");
		$array = array(	':titre' 	=> 	$this->Titre,
						':vente' => $journal->getPrixEdition(),
						':date'			=>	$this->Date,
						':url'		=>$this->URL,		
						':id_journal' => $this->IDRevue,
						':issn'			=>$this->ISSN,	
						':descriptif'	=>$this->Descriptif,
		);
		$stat->execute($array);
	}


} /* end of class Edition */

?>